package br.ita.battlefarm.views;

import java.util.ArrayList;
import java.util.List;

import br.ita.battlefarm.core.Game;
import br.ita.battlefarm.moves.Move;
import android.content.Context;
import android.widget.LinearLayout;

public class MovesListView extends LinearLayout {
	
	private ArrayList<MoveView> _moveViews = new ArrayList<MoveView>();
	private int _selectedMove = 1;

	public MovesListView(Context context) {
		super(context);
        setOrientation(LinearLayout.HORIZONTAL);
        
        List<Move> moves = Game.getInstance().generateAndRetrieveMoves();
        
        for(Move move : moves){
        	_moveViews.add(new MoveView(context, move));
        }
        
        for(MoveView view : _moveViews){
        	LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        	addView(view, layoutParams);
        }
	}
	
	public Move getSelectedMove(){
		return _moveViews.get(_selectedMove).getMove(); 
	}
	
	public void updateMoves(){
		invalidate();
		if(getSelectedMove().isMoveEnabled() == false){
			for(MoveView moveView : _moveViews){
				if(moveView.getMove().hasMovesLeft()){
					moveView.getMove().enableMove();
					_selectedMove = _moveViews.indexOf(moveView);
					break;
				}
			}
		}
		for(MoveView moveView : _moveViews){
			moveView.update();
		}
	}

	public void setSelectedMove(Move move) {
		for(MoveView moveView : _moveViews){
			if(moveView.getMove() == move){
				moveView.getMove().enableMove();
				_selectedMove = _moveViews.indexOf(moveView);
			}
			else{
				moveView.getMove().disableMove();
			}
		}
		updateMoves();
	}

}